<template>
  <div style="max-height: 600px;overflow-y: auto">
    <a-tree
      :allowClear="allowClear"
      :disabled="disabled"
      checkable
      checkStrictly
      :default-expand-all="true"
      :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
      :tree-data="options"
      @check="onCheck"
      :placeholder="placeholder"
      :show-line="true"
      v-model="value"
    >
    </a-tree>
  </div>

</template>

<script>
export default {
  model: {
    prop: 'outValue',
    event: 'change'
  },
  props: {
    outValue: {
      required: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      required: false,
      default: ''
    },
    expandTrigger: {
      type: String,
      required: false,
      default: 'hover'
    },
    allowClear: {
      type: Boolean,
      required: false,
      default: false
    },
    replaceFields: {
      type: Object,
      required: false,
      default: () => {
        return { children: 'children', title: 'name', key: 'id', value: 'id' }
        // return { label: 'name', value: 'id', children: 'children' }
      }
    }
  },
  data () {
    return {
      value: null,
      options: []
    }
  },
  watch: {},

  mounted () {
    console.log('执行了')
    this.getOptions()
  },
  methods: {
    convertOutValue () {
      if (this.outValue == null) {
        this.value = {
          checked: [],
          halfChecked: []
        }
      } else {
        this.value = {
          checked: this.outValue,
          halfChecked: []
        }
      }
    },

    // 获取数据
    getOptions () {
      this.$axios
        .get(this.$URLMAP.url.menu.query)
        .then((response) => {
          if (response.code === 200) {
            // let options = this.$util.filterRowChildren(response.data)
            const options = response.data
            this.options = [{
              title: '根目录',
              key: 0,
              value: 0,
              children: this.$util.treeTransition(
                options, { label: 'title', key: 'id', value: 'id' }
              )
            }]

            this.convertOutValue()
          }
        })
    },
    onCheck (checkedKeys) {
      this.value = checkedKeys
      this.$emit('change', checkedKeys.checked)
    }
  }
}
</script>
